package cn.nebula.music.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Time;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.nebula.music.domain.Song;
import cn.nebula.music.service.AlbumService;
import cn.nebula.music.service.ClassificationService;
import cn.nebula.music.service.CombinationService;
import cn.nebula.music.service.LyricsService;
import cn.nebula.music.service.MvService;
import cn.nebula.music.service.SingerService;
import cn.nebula.music.service.SongService;
import cn.nebula.music.service.SonglistService;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.MultimediaInfo;

@Controller
public class SongController {

	@Autowired
	private SongService songService;

	@Autowired
	private LyricsService lyricsService;

	@Autowired
	private AlbumService albumService;

	@Autowired
	private SingerService singerService;

	@Autowired
	private CombinationService combinationService;

	@Autowired
	private ClassificationService classificationService;

	@Autowired
	private MvService mvService;

	@Autowired
	private SonglistService songlistService;

	@RequestMapping("/editSong")
	@ResponseBody
	public Map<String, Object> editSong(@RequestParam Integer id) throws Exception {
		Map<String, Object> values = new HashMap<String, Object>();
		values.put("song", songService.findSongById(id));
		values.put("mvs", mvService.findAllMvsOfBT());
		values.put("lyricses", lyricsService.findAllLyricsesOfBT());
		values.put("albums", albumService.findAllAlbumsOfBT());
		values.put("singers", singerService.findAllSingersOfBT());
		values.put("combinations", combinationService.findAllCombinationsOfBT());
		values.put("classifications", classificationService.findAllClassificationsOfBT());
		values.put("songlists", songlistService.findAllSonglistsOfBT());
		return values;
	}

	@RequestMapping("/listAllSongs")
	@ResponseBody
	public Map<String, Object> listAllSongs() throws Exception {
		Map<String, Object> values = new HashMap<String, Object>();
		values.put("songs", songService.findAllSongsOfBT());
		return values;
	}

	@RequestMapping("/newSong")
	@ResponseBody
	public Map<String, Object> newSong() throws Exception {
		Map<String, Object> values = new HashMap<>();
		values.put("lyricses", lyricsService.findAllLyricsesOfBT());
		values.put("mvs", mvService.findAllMvsOfBT());
		values.put("albums", albumService.findAllAlbumsOfBT());
		values.put("singers", singerService.findAllSingersOfBT());
		values.put("combinations", combinationService.findAllCombinationsOfBT());
		values.put("classifications", classificationService.findAllClassificationsOfBT());
		values.put("songlists", songlistService.findAllSonglistsOfBT());
		return values;
	}

	@RequestMapping("/updateSong")
	@ResponseBody
	public boolean updateSong(@RequestBody Song song) throws Exception {
		songService.updateSong(song);
		return true;
	}

	@RequestMapping("/addSong")
	@ResponseBody
	public boolean addSong(@RequestBody Song song) throws Exception {
		songService.addSong(song);
		return true;
	}

	@RequestMapping("/deleteSong")
	@ResponseBody
	public boolean deleteSong(@RequestParam Integer id) throws Exception {
		songService.deleteSongById(id);
		return true;
	}

	@RequestMapping("/uploadSong")
	@ResponseBody
	public Time uploadSong(HttpServletRequest request, HttpServletResponse resp) {
		Time duration = null;
		try {
			String path = "d:\\Program Files\\nebula\\song";
			File dir = new File(path);
			if (!dir.exists()) {
				dir.mkdirs();
			}

			Part part = request.getPart("file");
			String fileName = path + "\\" + part.getSubmittedFileName();
			InputStream inputStream = part.getInputStream();
			OutputStream outputStream = new FileOutputStream(fileName);
			int ch = 0;
			while ((ch = inputStream.read()) != -1) {
				outputStream.write(ch);
			}
			outputStream.close();
			inputStream.close();

			Encoder encoder = new Encoder();
			File file = new File(fileName);
			MultimediaInfo m = encoder.getInfo(file);
			long ls = m.getDuration();
			int hour = (int) (ls / 1000 / 60 / 60);
			duration = new Time(ls);
			duration.setHours(hour);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(duration);
		return duration;
	}
}
